package ui.button 
{
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import ui.AnimatedBitmap;
	import ui.textfield.GCStylizedText;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;
	
	import org.flixel.FlxG;
	
	import flash.geom.Matrix;
	
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCButton extends Sprite
	{
		private var _text:TextField;
		
		protected var _buttonImage:AnimatedBitmap;	// The image for the button
		protected var _onToggle:Boolean;			// Used for checkbox-style behavior
		protected var _onT:GCStylizedText;			// Stores the 'on' or normal button state label
		protected var _offT:GCStylizedText;			// Stores the 'off' or highlighted button state label
		protected var _callback:Function;			// The function is called when the button is clicked
		protected var _pressed:Boolean;				// Tracks whether or not the button is currently pressed
		protected var _initialized:Boolean;			// Whether or not the button has initialized itself yet
		protected var _width:int;					// Width of this button
		protected var _height:int;					// Height of this button
		
		public function GCButton(callback:Function = null)
		{
			super();
			
			_buttonImage = null;
			_onT = null;
			_offT = null;
			_callback = callback;
			_onToggle = false;
			_pressed = false;
			_initialized = false;
			_width = 0;
			_height = 0;
			this.buttonMode = true;
			
			init();			
		}
		
		public function init():void 
		{
			
		}
		
				
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------		
		
		/**
		 * Set your own image as the button background
		 * @param	bmp				
		 * @param	width			The width of the image
		 * @param	height			The height of the image
		 */
		
		public function loadGraphic(imgClass:Class, width:int, height:int):void
		{
			_width = width;
			_height = height;
			var bmd:BitmapData = FlxG.addBitmap(imgClass);
			
			// Find the scaling factor
			var xScale:Number = width*4 / bmd.width;
			var yScale:Number = height / bmd.height;			
			var matrix:Matrix = new Matrix();
			matrix.scale(xScale, yScale);

			// Rescale the image
			var smallBMD:BitmapData = new BitmapData(bmd.width * xScale, bmd.height * yScale, true, 0x000000);
			smallBMD.draw(bmd, matrix, null, null, null, true);			
			_buttonImage = new AnimatedBitmap(smallBMD, width, height);
			
			// Add listeners to the button
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseEnter);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseExit);
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
			
			// Add the animated image
			_buttonImage.addAnimation("default", [0]);
			_buttonImage.addAnimation("indented", [1]);
			_buttonImage.addAnimation("highlight", [2]);
			_buttonImage.addAnimation("disabled", [3]);
			_buttonImage.play("default");
			addChild(_buttonImage);
		}
		
		/*
		public function loadGraphic(Image:FlxSprite,ImageHighlight:FlxSprite=null):FlxButton
		{
			_off = replace(_off,Image) as FlxSprite;
			if(ImageHighlight == null)
			{
				if(_on != _off)
					remove(_on);
				_on = _off;
			}
			else
				_on = replace(_on,ImageHighlight) as FlxSprite;
			_on.solid = _off.solid = false;
			_off.scrollFactor = scrollFactor;
			_on.scrollFactor = scrollFactor;
			width = _off.width;
			height = _off.height;
			refreshHulls();
			return this;
		}*/
		
		
		/**
		 * Updates the image of the button and renders the image
		 */
		public function update():void
		{
			if (_buttonImage != null)
			{
				_buttonImage.update();
				_buttonImage.render();
			}
		}
		
		/**
		 * Load a textfield into the button
		 * @param	text
		 */
		public function loadText(text:String):void
		{
			_onT = new GCStylizedText();
			_onT.text = text;
			_onT.setTextDimension(_width, _height);
			addChild(_onT);
			/*
			_onT = text;
			_onT.x = textX;
			_onT.y = textY;			
			_offT = textHightlight;			
			if (textHightlight != null) 
			{
				_offT.x = textX;
				_offT.y = textY;
			}
			
			this.addChild(_onT);*/
		}
		
		public function disable():void
		{
			if (_buttonImage != null)
			{
				this.alpha = 0.5;
				
				// Remove listeners to the button
				this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				this.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseEnter);
				this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseExit);
				this.removeEventListener(MouseEvent.CLICK, onMouseClick);
				_buttonImage.play("disabled");
				this.buttonMode = false;
				update();
			}

		}
		
		public function enable():void
		{
			if (_buttonImage != null)
			{
				this.alpha = 1.0;
				
				// Add listeners to the button
				this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				this.addEventListener(MouseEvent.MOUSE_OVER, onMouseEnter);
				this.addEventListener(MouseEvent.MOUSE_OUT, onMouseExit);
				this.addEventListener(MouseEvent.CLICK, onMouseClick);
				_buttonImage.play("default");
				this.buttonMode = true;
				update();
			}
		}
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------	
		
		// Handler for when user clicks on the button
		private function onMouseDown(event:MouseEvent):void
		{
			_buttonImage.play("indented");
			update();			
		}
		
		// Handler for when user releases the button
		private function onMouseUp(event:MouseEvent):void
		{
			_buttonImage.play("highlight");
			update();
		}
		
		// Handler for when the user is on the button
		private function onMouseEnter(event:MouseEvent):void
		{
			_buttonImage.play("highlight")
			update();
		}
		
		// Handler for when the user moves the mouse off the button
		private function onMouseExit(event:MouseEvent):void
		{
			_buttonImage.play("default")
			update();
		}
		
		// Handler for when the user actually clicks the button
		private function onMouseClick(event:MouseEvent):void
		{
			if (_callback != null)
			{
				_callback(event);
			}
		}
		
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------			
		
		public function get on():Boolean
		{
			return _onToggle;
		}
		
		//----------------------------------------------------------------------
		//	SET FUNCTIONS
		//----------------------------------------------------------------------			
		
		public function set on(on:Boolean):void
		{
			_onToggle = on;
		}
		
		public function set callback(newFunction:Function):void
		{
			_callback = newFunction;
		}
	}
}